package org.example;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ycd
 */
public class ProblemTwo {
//    之前写法扩展起来有点麻烦，换种写法
    public static final Map<String, Double> FRUIT_PRICES = new HashMap<String, Double>() {{
        put("苹果", 8.0);
        put("草莓", 13.0);
        put("芒果", 20.0);
    }};

    public static double calculateTotalPrice(Map<String, Integer> fruitNums) {
        double total = 0.0;
        for (Map.Entry<String, Integer> entry : fruitNums.entrySet()) {
            String fruitName = entry.getKey();
            int weight = entry.getValue();
            if (FRUIT_PRICES.containsKey(fruitName)) {
                double price = FRUIT_PRICES.get(fruitName);
                total += weight * price;
            } else {
                System.out.println("无此水果");
            }
        }
        return total;
    }

    public static void main(String[] args) {
        // 测试 顾客购买了1斤苹果，2斤草莓，和3斤芒果
        Map<String, Integer> fruitNums = new HashMap<String, Integer>() {{
            put("苹果", 1);
            put("草莓", 2);
            put("芒果", 3);
        }};

        double totalPrice = calculateTotalPrice(fruitNums);
        System.out.printf("顾客B购买的水果总价为 %.2f 元。\n", totalPrice);
    }


}